var cur_hash = ''; // 当前hash值，保存用于tab的跳转

$(document).ready(function() {
    if (('onhashchange' in window) && ((typeof document.documentMode==='undefined') || document.documentMode==8)) {
        // 浏览器支持onhashchange事件
        window.onhashchange = hash_changed;
    } else {
        // 不支持则用定时器检测的办法
        setInterval(function() {
            var ischanged = is_hash_changed();
            if(ischanged) {
                hash_changed();
            }
        }, 150);
    }

    $('.help_sidebar a').click(function(e) {
        href = $(this).attr('href');
        location.hash = href;

        scrollTo(0, 0);
    });

    // url redirect
    hash_changed();
    
    $('#feedback form').submit(function() {
        if (check_feedback()) {
            submit_feedback();
        }
        return false;
    });
    
    scrollTo(0, 0);    
});

// 检测hash是否变化
function is_hash_changed() {
    hash = location.hash;
    if (hash !== cur_hash) {
        cur_hash = hash;
        return true;
    } else {
        return false;
    }
}

// hash变化后触发
function hash_changed() {
    switch (location.hash) {
        case '':
        case '#':
        case '#about':
            $('.help_sidebar a[href=#about]').click();
            break;
        case '#guide':
            $('.help_sidebar a[href=#guide]').click();
            break;
        case '#regulation':
            $('.help_sidebar a[href=#regulation]').click();
            break;
        case '#privacy':
            $('.help_sidebar a[href=#privacy]').click();
            break;
        case '#contact':
            $('.help_sidebar a[href=#contact]').click();
            break;
        case '#feedback':
            $('.help_sidebar a[href=#feedback]').click();
            break;
        default:
            break;
    }
}

function check_feedback() {
    $('.error').removeClass('error');
    $('.help-inline').hide();
    var pass_check = true;
    
    var $email = $('#fb_email');
    var $email_error = $email.next('.help-inline');
    $email_error.text('请填写您的邮箱');
    if ($email.val() != '' && !EB.util.is_email($email.val())) {
        $email.parents('.control-group').addClass('error');
        $email_error.text('请输入正确的邮箱').show();
        pass_check = false;        
    }
    
    $('#feedback .input-large').each(function() {
        if ($(this).val() == '') {
            $(this).parents('.control-group').addClass('error');
            $(this).next('.help-inline').show();
            pass_check = false;
        }
    });
    
    return pass_check;
}

function submit_feedback() {
    $.post('help/do_feedback', {
        fb_name: $('#fb_name').val(),
        fb_email: $('#fb_email').val(),
        fb_title: $('#fb_title').val(),
        fb_content: $('#fb_content').val()
    }, function(data) {
        if (data == 'OK') {
            $('#fb_submit').show();
            $('.error').removeClass('error');
            $('.help-inline').hide();
            $('#feedback input').each(function() {
                $(this).val('');
            });
            $('#feedback textarea').val('');
        } else {
            alert('反馈提交失败，请重试！');
        }
    }, 'text');
}